package com.dodock.banglapapers.server.helper;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListInqilab implements DDNNewsListInterface {
	
	@Override
	public String GenerateNewsList(String categoryid) {
		try {
			String url = "";
			if (categoryid.equalsIgnoreCase("lead")) {
				url = Settings.WWW_INQILAB_COM + "/index.php";
			} else if( categoryid.equalsIgnoreCase("editorial")){
				url = Settings.WWW_INQILAB_COM + "/editorial.php?id=10&&page_id=10";
			} else if( categoryid.equalsIgnoreCase("national")){
				url = Settings.WWW_INQILAB_COM + "/national.php?id=5&&page_id=5";
			} else{
				url = String.format(Settings.WWW_INQILAB_COM + "/innerPage.php?id=%s&&page_id=%s" , categoryid, categoryid);
			}
			return getNewsList(url, categoryid);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}

	private String getNewsList(String url, String categoryid) {
		JsonArray array = new JsonArray();
		Document doc;
		try {
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(60000).execute();
			doc = response.parse();

			if (categoryid.equalsIgnoreCase("lead")
					|| categoryid.equalsIgnoreCase("editorial")
					|| categoryid.equalsIgnoreCase("national")) {
				generateLeadNewsList(doc, array);
			} else {
				generateCategoryNewsList(doc, array);
			}
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}
	
	private void generateCategoryNewsList(Document doc, JsonArray array) {
		
		Elements leadTitleElements = doc.select("td[class=news-headline]");
		Elements imageElements = doc.select("td[class=short-news-title]");
		
		parseLeadElements(leadTitleElements, imageElements, array);

		
		leadTitleElements = null;
		leadTitleElements = doc.select("div[class=inner-heading underline left10px]");
		imageElements = null;
		imageElements = doc.select("div[class=news-inner left10px]");
		
		parseLeadElements(leadTitleElements, imageElements, array);
	}
	
	private void generateLeadNewsList(Document doc, JsonArray array) {
		Elements slideNewsObjs = doc.select("div[class=bdnSlide]");
		for(Element slideNews : slideNewsObjs){
			parseElement(slideNews, array);
		}

		Elements boxNewsElements = doc.select("div[id=resultatss]");
		for(Element boxNewElement : boxNewsElements){
			parseElement(boxNewElement, array);
		}
		
		Elements bottomListNews = doc.select("span[class=feature-home underline left10px]");
		for(Element listNews : bottomListNews){
			parseElement(listNews, array);
		}
	}

	
	private void parseElement(Element element, JsonArray array){
		
		JsonObject obj = new JsonObject();
		String id = element.select("a").first().attr("href");		
		String title = element.select("a").first().text();
		
		Elements imageElements = element.select("img");
		if(!imageElements.isEmpty()){
			String image = element.select("img").first().attr("src");
			obj.addProperty("image", image);
		}
		obj.addProperty("id", encode(id));
		obj.addProperty("title", title);
		
		array.add(obj);
	}
	
	private void parseLeadElements(Elements leadTitleElements,
			Elements imageElements, JsonArray array) {
		JsonObject obj;
		String id, title, image;

		for(int i = 0; i < leadTitleElements.size(); i++){
			obj = new JsonObject();
			id = leadTitleElements.get(i).select("a").first().attr("href");		
			title = leadTitleElements.get(i).select("a").first().text();
			
			obj.addProperty("id", encode(id));
			obj.addProperty("title", title);
			
			if(!imageElements.get(i).select("img").isEmpty()){
				image = imageElements.get(i).select("img").first().attr("src");
				obj.addProperty("image", image);
			}

			array.add(obj);
		}
	}
	
	
	private String encode(String id){
		id = id.replaceAll(" ", "%20");
		id = id.replaceAll("&", "%26");
		return id;
	}
}
